package com.hrm.util;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import com.hrm.entity.Menu;


public class JSONUtil {
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static String parseMap(Map map) {
		StringWriter out = new StringWriter();
		JSONObject obj = new JSONObject();
		Set<String> key = map.keySet();
		for (Iterator<String> it = key.iterator(); it.hasNext();) {
	            String s = (String) it.next();
	            obj.put(s, map.get(s));
	        }
		try {
			obj.writeJSONString(out);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return obj.toJSONString();
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static String parseList(List list){
		JSONArray ja = new JSONArray();
		ja.add(list);
		return ja.toJSONString();
	}
	

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Map<String,Menu> map = new HashMap<String,Menu>();
		Menu menu = new Menu();
		menu.setMenuName("test");
		menu.setViewMode("Y");
		Menu menu1 = new Menu();
		menu1.setMenuName("test1");
		menu1.setViewMode("Y");
		List<Menu> childNodes = new ArrayList<Menu>();
		childNodes.add(menu);
		childNodes.add(menu1);
		System.out.println(JSONUtil.parseList(childNodes));
		JSONArray jsonArray = new JSONArray();
		jsonArray.add(childNodes);
		System.out.println(jsonArray.toJSONString());
		
		map.put("menu1", menu);
		map.put("menu2", menu1);
		System.out.println(JSONUtil.parseMap(map));
		
	}

}
